<?php
/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
error_reporting(E_ALL^E_NOTICE^E_WARNING);
include_once( 'BaseController.php' ); 
class OrderController extends BaseController {

	
	//确认收货
	public function  finishAction(){
	
		$user=$this->checklogin2();
		
		$info=json_decode(file_get_contents("php://input"),true);
		
		$orderid=$info['orderid'] ? $info['orderid'] : $_REQUEST['orderid'];
		
		$ordermodel=new OrderModel();
		$where['oid=?']=$orderid;
		$order_one=$ordermodel->getone($where);
		if(!$order_one){
			exit(json_encode(array('status'=>0,'info'=>'订单不存在')));
		}
		if($order_one[status]==4){
			exit(json_encode(array('status'=>0,'info'=>'此订单已完成')));
		}
		
		if($order_one[status]!=3){
			exit(json_encode(array('status'=>0,'info'=>'此订单未发货')));
		}
		
		$data['status']=4;
		$data['ftime']=time();
		
	
		if($ordermodel->updateorder($data, $where)){
			$order_one=$ordermodel->getone($where);
			exit(json_encode(array('status'=>1,'info'=>'修改订单状态成功','order'=>$order_one)));
		}else{
			exit(json_encode(array('status'=>0,'info'=>'修改失败')));
		}
		
	}
	
	
	
	//确认下单
	public function confirmAction(){
	    $user=$this->checklogin2();
		
		$info=json_decode(file_get_contents("php://input"),true);
	
// 		$info=array(
// 			 'salt'=>'0b8c1500112432e64187cd8eaf9e7fef',
// 			 'orderid'=>150,
// 			 'remark'=>'口味重些',
// 			 'paytype'=>'1',
// 		);
		
//echo json_encode($info); echo 123; die();
   //  print_r($info);
	    
	    $orderid=$info['orderid'] ? $info['orderid'] : $_REQUEST['orderid'];
	   
		$ordermodel=new OrderModel();
		$where['oid=?']=$orderid;
		$order_one=$ordermodel->getone($where);
		if(!$order_one){
			exit(json_encode(array('status'=>0,'info'=>'订单不存在')));
		}
		
		$where2['orderid=?']=$orderid;
		$order_goods_list=$ordermodel->order_goods_list($where2);
		if(!$order_goods_list){
			exit(json_encode(array('status'=>0,'info'=>'商品不存在')));
		}
		
		$data['remark']=$info['remark'];
		$data['paytype']=$info['paytype'];
		$data['status']=1;
		$where['oid =?']=$info['orderid'];
		
		if($ordermodel->updateorder($data, $where)){
			$order_one=$ordermodel->getone($where);
			exit(json_encode(array('status'=>1,'info'=>'修改订单状态成功','order'=>$order_one)));
		}else{
			exit(json_encode(array('status'=>0,'info'=>'修改失败')));
		}
		
	}
	
	public function detailAction(){
		$user=$this->checklogin2();
		
		$info=json_decode(file_get_contents("php://input"),true);
		$orderid=$info['orderid'] ? $info['orderid'] : $_REQUEST['orderid'];
		$ordermodel=new OrderModel();
		$where['oid=?']=$orderid;
		$order_one=$ordermodel->getone($where);
		if(!$order_one){
			exit(json_encode(array('status'=>0,'info'=>'订单不存在')));
		}
		
		$sellermodel=new SellerModel();
		$swhere['id=?']=$order_one['sellerid'];
		$sellerone=$sellermodel->getone($swhere);
		if(!$sellerone){
			exit(json_encode(array('status'=>0,'info'=>'商家不存在')));
		}
		
		$where2['orderid=?']=$orderid;
		$order_goods_list=$ordermodel->order_goods_list($where2);
		if(!$order_goods_list){
			exit(json_encode(array('status'=>0,'info'=>'商品不存在')));
		}
		
		$goodsmodel=new GoodsModel();
		foreach ($order_goods_list as &$val){
			$gwhere['id=?']=$val['goodsid'];
			$goodsone=$goodsmodel->getone($gwhere);
			// print_r($goodsone);
			// $val['id']=$goodsone['id'];
			$val['sellerid']=$goodsone['sellerid'];
			$val['goods_categoryid']=$goodsone['goods_categoryid'];
			$val['name']=$goodsone['name'];
			$val['desc']=$goodsone['desc'];
			$val['currentprice']=$goodsone['currentprice'];
			$val['thumb']=$goodsone['thumb'];
		
		
			//$price=$price+($val['total']*$goodsone['currentprice']);
		
		}
		
		$sellerone['price']=$order_one['price'];
		$sellerone['ctime']=$order_one['ctime'];
		$sellerone['total_price']=$order_one['price']+$sellerone['send_price'];
		$sellerone['order_info']=$order_goods_list;
		exit(json_encode($sellerone));
		//$order_goods=
		//echo $this->salt_time; die();
	}
	
	
	public function listAction(){
		$info=json_decode(file_get_contents("php://input"),true);
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
		$status=$info['status'] ? $info['status'] : $_REQUEST['status'];
		$page=$info['page'] ? $info['page'] : $_REQUEST['page'];
		
		$pagesize=$info['pagesize'] ? $info['pagesize'] : $_REQUEST['pagesize'];
		
	    if(!$salt){exit(json_encode(array('status'=>0,'info'=>'参数不得为空')));}
		$where['salt=?']=$salt;
		
		
		$usermodel=new UserModel();
		$userone=$usermodel->getuser($where);
		if(!$userone){exit(json_encode(array('status'=>0,'info'=>'用户凭证校验失败')));}
		
		if(isset($status)){
			if($status=='no0'){
				$owhere['status >?']=0;
			}else{
				$owhere['status=?']=$status;
			}
		}
		
		$owhere['userid=?']=$userone['id'];
		$ordermodel = new OrderModel();
		$list = $ordermodel->getList($owhere,$page,$pagesize);
		
		
		echo json_encode($list);
	}
	
	public function getAction(){
	
	}
	
	public function cartAction(){
	$info=json_decode(file_get_contents("php://input"),true);
// 		$info=array(
// 		'salt'=>'da21b22f7208059c3adbd2095542ee9c',
// 		'seller_id'=>2,
// 		'goods'=>array(
// 			array('goodsid'=>10,'total'=>1),
// 			array('goodsid'=>13,'total'=>2)
// 		));
// 		echo json_encode($info); die();
		if(!$info){exit(json_encode(array('status'=>0,'info'=>'参数不得为空')));}
		$usermodel=new UserModel();
		$where['salt=?']=$info['salt'];
		$userone=$usermodel->getuser($where);
		if(!$userone){exit(json_encode(array('status'=>0,'info'=>'用户凭证校验失败')));}
	//	if(!$userone['address']){exit(json_encode(array('status'=>0,'info'=>'请先填写用户地址')));}
		$sellermodel=new SellerModel();
		$swhere['id=?']=$info['seller_id'];
		$sellerone=$sellermodel->getone($swhere);
		if(!$sellerone){exit(json_encode(array('status'=>0,'info'=>'商家不存在')));}
		$goodsmodel=new GoodsModel();
		
		foreach ($info['goods'] as &$val){
			$gwhere['id=?']=$val['goodsid'];
			$goodsone=$goodsmodel->getone($gwhere);
		   // print_r($goodsone);
		   // $val['id']=$goodsone['id'];
		    $val['sellerid']=$goodsone['sellerid'];
		    $val['goods_categoryid']=$goodsone['goods_categoryid'];
		    $val['name']=$goodsone['name'];
		    $val['desc']=$goodsone['desc'];
		    $val['currentprice']=$goodsone['currentprice'];
		    $val['thumb']=$goodsone['thumb'];
		  
		    
			$price=$price+($val['total']*$goodsone['currentprice']);
				
		}
		//$sellerone['goods']=
		//print_r($info['goods']);
		$sellerone['goods']=$info['goods'];
		$sellerone['price']=$price;
		//print_r($sellerone);
		exit(json_encode($sellerone));
	}
	
	

	
	public function addAction(){
		
		
		
	$info=json_decode(file_get_contents("php://input"),true);
// 		$info=array(
// 			'salt'=>'0b8c1500112432e64187cd8eaf9e7fef',
// 			'seller_id'=>2,
// 			'goods'=>array(
// 					array('goodsid'=>10,'total'=>1),
// 					array('goodsid'=>13,'total'=>2)
// 		));
// 		$info=array(
// 		'salt'=>'b66a0ebdf437ea30e345fe2e3f815d76',
// 		'seller_id'=>2,	
// 		'remark'=>'口味重些',
// 		'paytype'=>'1',					
// 		'goods'=>array(
// 			array('goodsid'=>10,'total'=>1),
// 			array('goodsid'=>13,'total'=>2)
// 		));
		//echo json_encode($info); die();
		//{"salt":"9c46955967bdfff0b66e8d65cdece7b4","seller_id":2,"remark":"\u53e3\u5473\u91cd\u4e9b","paytype":"1","goods":[{"goodsid":10,"total":1},{"goodsid":13,"total":2}]}
		if(!$info){exit(json_encode(array('status'=>0,'info'=>'参数不得为空')));}
		$usermodel=new UserModel();
		$where['salt=?']=$info['salt'];
		$userone=$usermodel->getuser($where);
		if(!$userone){exit(json_encode(array('status'=>0,'info'=>'用户凭证校验失败')));}
		
		//if(!$userone['address']){exit(json_encode(array('status'=>0,'info'=>'请先填写用户地址')));}
		$sellermodel=new SellerModel();
		$swhere['id=?']=$info['seller_id'];
		$sellerone=$sellermodel->getone($swhere);
		//print_r($sellerone); die();
		if(!$sellerone){exit(json_encode(array('status'=>0,'info'=>'商家不存在')));}
		$goodsmodel=new GoodsModel();
		
		//print_r($info['goods']);
		//$zenddb=new Zend_Db_Table();
		//$db=$zenddb->getAdapter();
		$info_goods=$info['goods'];
		
		foreach ($info_goods as &$val){
			$gwhere['id=?']=$val['goodsid'];
			$goodsone=$goodsmodel->getone($gwhere);
		
			$price=$price+($val['total']*$goodsone['currentprice']);
			
			$val['sellerid']=$goodsone['sellerid'];
			$val['goods_categoryid']=$goodsone['goods_categoryid'];
			$val['name']=$goodsone['name'];
			//$val['desc']=$goodsone['desc'];
			$val['currentprice']=$goodsone['currentprice'];
			$val['thumb']=$goodsone['thumb'];
		} 
		$price=sprintf("%.2f",$price);
		$sellerone['goods']=$info_goods;
		//exit(json_encode($sellerone));
		$odata['price']=sprintf("%.2f",$price);
		$odata['total_price']=sprintf("%.2f",$price+$sellerone['send_price']);
		$odata['ordersn']='qunbang'.date('YmdHis') . $this->random(6, true);
		$ordersn=$odata['ordersn'];
		$odata['userid']=$userone['id'];
		$odata['sellerid']=$info['seller_id'];
		$odata['status']=0;
		$odata['address']=$userone['address'];
		$odata['ctime']=time();
		$odata['remark']=$info['remark'];
		$odata['paytype']=$info['paytype'];
		$ordermodel=new OrderModel();
		
		unset($val);
		if($ordermodel->addorder($odata)){
			$oid=$ordermodel->getlastid();
			
			foreach ($info['goods'] as $val){
				$gwhere['id=?']=$val['goodsid'];
				$goodsone=$goodsmodel->getone($gwhere);
				$ogdata['orderid']=$oid;
				$ogdata['goodsid']=$val['goodsid'];
				$ogdata['price']=$goodsone['currentprice'];
				$ogdata['total']=$val['total'];
				$ogdata['userid']=$userone['id'];
				$ogdata['ctime']=time();
			    $ordermodel->addorder_goods($ogdata);
			}
			
			
			exit(json_encode(array('status'=>1,'info'=>'生成用户订单成功','oid'=>$oid,'ordersn'=>$ordersn,'price'=>$price,'user'=>$userone,'seller'=>$sellerone)));
			
		}else{
			exit(json_encode(array('status'=>0,'info'=>'生成用户订单失败')));
		}
	   
		
	}
	
	public function random($length, $numeric = FALSE) {
		$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
		$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
		if ($numeric) {
			$hash = '';
		} else {
			$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
			$length--;
		}
		$max = strlen($seed) - 1;
		for ($i = 0; $i < $length; $i++) {
			$hash .= $seed{mt_rand(0, $max)};
		}
		return $hash;
	}
	
	public function editAction(){
	
	}
	
	public function delAction(){
	
	}
	
	public function statusAction(){
	
	}
	
	
	public function goodsListAction(){
		
	    $info=json_decode(file_get_contents("php://input"),true);
		$id=$info['id'] ? $info['id'] : $_REQUEST['id'];
		$sellerid=$info['sellerid'] ? $info['sellerid'] : $_REQUEST['sellerid'];
		$goods_categoryid=$info['goods_categoryid'] ? $info['goods_categoryid'] : $_REQUEST['goods_categoryid'];
		$name=$info['name'] ? $info['name'] : $_REQUEST['name'];
		$page=$info['page'] ? $info['page'] : $_REQUEST['page'];
		$pagesize=$info['pagesize'] ? $info['pagesize'] : $_REQUEST['pagesize'];
		//$goodsname=$info['goodsname'] ? $info['goodsname'] : $_REQUEST['goodsname'];
		
		$where=" 1=1 ";
		
		if($id){
			$where.=" and id = '$id' ";
		}
		
		if($sellerid){
			
			$where.=" and sellerid = '$sellerid' ";
		}
		
		if($goods_categoryid){
				
			$where.=" and goods_categoryid = '$goods_categoryid' ";
		}
		
		
		
		if($name){
		
			$where.=" and name like '%".$name."%' ";
		}
		
		
		$model = new GoodsModel();
		$list = $model->getList($where,$page,$pagesize);
		echo json_encode($list);
	}
	
	
	
}
